package com.freecloud.bitorrent.domain.server.command;

import com.freecloud.bitorrent.constant.LocalNodeConstant;
import com.freecloud.bitorrent.convert.BitTorrentConvert;
import com.freecloud.bitorrent.demo.bo.GetPeersRequest;
import com.freecloud.bitorrent.demo.bo.GetPeersResponse;
import com.freecloud.bitorrent.demo.bo.RequestMessage;
import com.freecloud.bitorrent.demo.bo.ResponseMessage;
import com.freecloud.bitorrent.domain.server.support.DhtCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Author: maomao
 * @Date: 2022/7/23 4:53 PM
 */
@Slf4j
@Component("get_peers")
public class GetPeersCommand implements DhtCommand {
    @Override
    public ResponseMessage response(RequestMessage requestMessage) {
        log.info("get_peers requestMessage : {}", requestMessage);
        ResponseMessage responseMessage = BitTorrentConvert.INSTANCE.request2Response(requestMessage);
        GetPeersRequest peersRequest = (GetPeersRequest)responseMessage.getResult();
        responseMessage.setResult(new GetPeersResponse(LocalNodeConstant.ip, peersRequest.getInfoHash()));
        return responseMessage;
    }
}
